//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "TreeSwaptionEngine.h"
using namespace Cephei::QL::Pricingengines::Swaption;
#include <gen/QL/Models/ShortRateModel.h>
#include <gen/QL/TimeGrid.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Models;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::CTreeSwaptionEngine (Cephei::QL::Models::IShortRateModel^ model, Cephei::QL::ITimeGrid^ timeGrid, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ termStructure) : CPricingEngine(CTreeSwaptionEngine::typeid)
{
    CShortRateModel^ _Cmodel;
    CTimeGrid^ _CtimeGrid;
    CYieldTermStructure^ _CtermStructure;
    try
    {
#ifdef HANDLE
        _phTreeSwaptionEngine = NULL;
#endif
        _Cmodel = safe_cast<CShortRateModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::ShortRateModel>& _model = static_cast<boost::shared_ptr<QuantLib::ShortRateModel>&> (_Cmodel->GetShared ()); 
        _CtimeGrid = safe_cast<CTimeGrid^> (timeGrid);
        _CtimeGrid->Lock();
        QuantLib::TimeGrid& _timeGrid = static_cast<QuantLib::TimeGrid&> (_CtimeGrid->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (termStructure))
        {
            _CtermStructure = safe_cast<CYieldTermStructure^> (termStructure->Value);
            _CtermStructure->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _termStructure = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (termStructure) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CtermStructure->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppTreeSwaptionEngine = new boost::shared_ptr<QuantLib::TreeSwaptionEngine> (new QuantLib::TreeSwaptionEngine ( _model,  _timeGrid,  _termStructure ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppTreeSwaptionEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
        if (_CtimeGrid != nullptr) _CtimeGrid->Unlock();
        if (_CtermStructure != nullptr) _CtermStructure->Unlock();
    }
}
Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::CTreeSwaptionEngine (Cephei::QL::Models::IShortRateModel^ model, UInt64 timeSteps, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ termStructure) : CPricingEngine(CTreeSwaptionEngine::typeid)
{
    CShortRateModel^ _Cmodel;
    CYieldTermStructure^ _CtermStructure;
    try
    {
#ifdef HANDLE
        _phTreeSwaptionEngine = NULL;
#endif
        _Cmodel = safe_cast<CShortRateModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::ShortRateModel>& _model = static_cast<boost::shared_ptr<QuantLib::ShortRateModel>&> (_Cmodel->GetShared ()); 
        QuantLib::Size _timeSteps = (QuantLib::Size)ValueHelper::Convert (timeSteps);
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (termStructure))
        {
            _CtermStructure = safe_cast<CYieldTermStructure^> (termStructure->Value);
            _CtermStructure->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _termStructure = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (termStructure) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CtermStructure->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppTreeSwaptionEngine = new boost::shared_ptr<QuantLib::TreeSwaptionEngine> (new QuantLib::TreeSwaptionEngine ( _model,  _timeSteps,  _termStructure ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppTreeSwaptionEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
        if (_CtermStructure != nullptr) _CtermStructure->Unlock();
    }
}
Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::CTreeSwaptionEngine (boost::shared_ptr<QuantLib::TreeSwaptionEngine>& childNative, Object^ owner) : CPricingEngine(CTreeSwaptionEngine::typeid)
{
#ifdef HANDLE
	_phTreeSwaptionEngine = NULL;
#endif
	_ppTreeSwaptionEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppTreeSwaptionEngine));
}
Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::CTreeSwaptionEngine (QuantLib::TreeSwaptionEngine& childNative, Object^ owner) : CPricingEngine(CTreeSwaptionEngine::typeid)
{
#ifdef HANDLE
	_phTreeSwaptionEngine = NULL;
#endif
	_ppTreeSwaptionEngine = new boost::shared_ptr<QuantLib::TreeSwaptionEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppTreeSwaptionEngine));
    _TreeSwaptionEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::CTreeSwaptionEngine (CTreeSwaptionEngine^ copy) : CPricingEngine(CTreeSwaptionEngine::typeid)
{
#ifdef HANDLE
	_phTreeSwaptionEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTreeSwaptionEngine = new boost::shared_ptr<QuantLib::TreeSwaptionEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppTreeSwaptionEngine));
    }
}
Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::CTreeSwaptionEngine (System::Type^ t) : CPricingEngine(CTreeSwaptionEngine::typeid)
{
#ifdef HANDLE
	_phTreeSwaptionEngine = NULL;
#endif
	if (!t->IsSubclassOf(CTreeSwaptionEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::CTreeSwaptionEngine (QuantLib::Handle<QuantLib::TreeSwaptionEngine>& childNative, Object^ owner)  : CPricingEngine(CTreeSwaptionEngine::typeid)
{
	_phTreeSwaptionEngine = &childNative;
	_ppTreeSwaptionEngine = &static_cast<boost::shared_ptr<QuantLib::TreeSwaptionEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppTreeSwaptionEngine));
    _TreeSwaptionEngineOwner = owner;
}
Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::CTreeSwaptionEngine (QuantLib::Handle<QuantLib::TreeSwaptionEngine> childNative)  : CPricingEngine(CTreeSwaptionEngine::typeid)
{
	_phTreeSwaptionEngine = &childNative;
	_ppTreeSwaptionEngine = &static_cast<boost::shared_ptr<QuantLib::TreeSwaptionEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppTreeSwaptionEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::CTreeSwaptionEngine (QuantLib::TreeSwaptionEngine childNative)  : CPricingEngine(CTreeSwaptionEngine::typeid)
{
#ifdef HANDLE
	_phTreeSwaptionEngine = NULL;
#endif
	_ppTreeSwaptionEngine = new boost::shared_ptr<QuantLib::TreeSwaptionEngine> (new QuantLib::TreeSwaptionEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppTreeSwaptionEngine));
}
#endif

Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::~CTreeSwaptionEngine ()
{
    if (_ppTreeSwaptionEngine != NULL)
    {
	    delete _ppTreeSwaptionEngine;
        _ppTreeSwaptionEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::!CTreeSwaptionEngine ()
{
    if (_ppTreeSwaptionEngine != NULL)
    {
	    delete _ppTreeSwaptionEngine;
    }
}
QuantLib::TreeSwaptionEngine& Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::GetReference ()
{
    if (_ppTreeSwaptionEngine == NULL) throw gcnew NativeNullException ();
	return **_ppTreeSwaptionEngine;
}
boost::shared_ptr<QuantLib::TreeSwaptionEngine>& Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::GetShared ()
{
    if (_ppTreeSwaptionEngine == NULL) throw gcnew NativeNullException ();
	return *_ppTreeSwaptionEngine;
}
QuantLib::TreeSwaptionEngine* Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::GetPointer ()
{
    if (_ppTreeSwaptionEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppTreeSwaptionEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TreeSwaptionEngine>& Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::GetHandle ()
{
	if (_phTreeSwaptionEngine == NULL)
	{
		_phTreeSwaptionEngine = new Handle<QuantLib::TreeSwaptionEngine> (*_ppTreeSwaptionEngine);
	}
	return *_phTreeSwaptionEngine;
}
#endif
bool Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::HasNative () 
{
	return (_ppTreeSwaptionEngine != NULL);
}

Cephei::QL::Pricingengines::Swaption::ITreeSwaptionEngine^ Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine::Calculate::get ()
{
    try
    {
    	(*_ppTreeSwaptionEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Swaption::ITreeSwaptionEngine^ Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine_Factory::Create (Cephei::QL::Models::IShortRateModel^ model, Cephei::QL::ITimeGrid^ timeGrid, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ termStructure)
{
    return gcnew CTreeSwaptionEngine ( model,  timeGrid,  termStructure);
}
Cephei::QL::Pricingengines::Swaption::ITreeSwaptionEngine^ Cephei::QL::Pricingengines::Swaption::CTreeSwaptionEngine_Factory::Create (Cephei::QL::Models::IShortRateModel^ model, UInt64 timeSteps, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ termStructure)
{
    return gcnew CTreeSwaptionEngine ( model,  timeSteps,  termStructure);
}
